package com.hl.card.modular.packBack.controller;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.hl.card.common.controller.BaseController;
import com.hl.card.common.persistence.model.Recharge;
import com.hl.card.common.persistence.model.User;
import com.hl.card.core.log.LogObjectHolder;
import com.hl.card.modular.pack.service.UserDataService;
import com.hl.card.modular.system.service.IUserService;
import com.hl.card.modular.system.service.impl.UserService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 卡包用户管理
 */
@Controller
@RequestMapping("/cardPackUser")
public class CardPackUserController  extends BaseController {

    private String PREFIX = "/packback/cardPackUser/";

    @Autowired
    private UserDataService userService;
    /**
     * 跳转到首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "user.html";
    }
    /**
     * 跳转到添加
     */
    @RequestMapping("/user_add")
    public String userAdd() {
        return PREFIX + "user_add.html";
    }
    /**
     * 跳转到修改
     */
    @RequestMapping("/user_update/{userId}")
    public String userUpdate(@PathVariable Integer userId, Model model) {
        User user = userService.selectById(userId);
        model.addAttribute("item",user);
        LogObjectHolder.me().set(user);
        return PREFIX + "user_edit.html";
    }
    /**
     * 获取列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(
                User user,
            @RequestParam(required = false)  String beginTime,
            @RequestParam(required = false)  String endTime,
             @RequestParam(required = false,name = "uname")  String uname
    ) {
        user.setName(uname);
        Page<User> dPage = userService.selectMultiCondition(user,beginTime,endTime);
        List<User> userList=dPage.getRecords();
        userList.forEach(user1->{
            user1.setPassword(null);
            user1.setSalt(null);
            user1.setUserPayPassword(null);
         });
        Map<String, Object> jsonMap = new HashMap<String, Object>();
        jsonMap.put("total", dPage.getTotal());
        jsonMap.put("rows", userList);
        return jsonMap;
    }
    /**
     * 新增
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(User user) {
        userService.insert(user);
        return SUCCESS_TIP;
    }
    /**
     * 删除
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer userId) {
        userService.deleteById(userId);
        return SUCCESS_TIP;
    }
    /**
     * 修改
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(User user) {
        userService.updateById(user);
        return SUCCESS_TIP;
    }
    /**
     * 详情
     */
    @RequestMapping(value = "/detail/{userId}")
    @ResponseBody
    public Object detail(@PathVariable("userId") Integer userId) {
        return userService.selectById(userId);
    }

}
